package com.ruisi.weatherreport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.ruisi.operateClasses.*;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.content.ContentValues;
import android.database.Cursor;   
import android.database.sqlite.SQLiteDatabase;


public class SqliteActivity extends Activity{
	
	List<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();//list用于绑定SQLitedatabase数据
	HashMap<String,Object> map=new HashMap<String,Object>();
	listViewAdapter mAdapter;
	
	 private Button createDatabaseButton;//创建数据库按钮
	 private Button updateDatabaseButton;//更新数据库按钮
	 private Button insertButton;//插入数据按钮
	 private Button selectButton;//查询数据按钮
	 private Button deleteButton;//删除数据按钮
	 private Button updateButton;//更新数据按钮
	 private ListView lv_showSQLitedata;//显示SQLite数据库信息
	 
	 protected void onCreate(Bundle savedInstanceState){
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.sqlitedatabase);
		 
		 //实例化按钮
		 createDatabaseButton=(Button)findViewById(R.id.createDatabase);
		 updateDatabaseButton=(Button)findViewById(R.id.updateDatabase);
		 insertButton=(Button)findViewById(R.id.insert);
		 selectButton=(Button)findViewById(R.id.select);
		 deleteButton=(Button)findViewById(R.id.delete);
		 updateButton=(Button)findViewById(R.id.update);
		 lv_showSQLitedata=(ListView)findViewById(R.id.lv_showSQLitedata);
		 
		 //设置按钮监听器
		 createDatabaseButton.setOnClickListener(new createDatabaseOnClicklistner());
		 updateDatabaseButton.setOnClickListener(new updateDatabaseOnClicklistner());
		 insertButton.setOnClickListener(new insertOnClicklistner());
		 selectButton.setOnClickListener(new selectOnClicklistner());
		 deleteButton.setOnClickListener(new deleteOnClicklistner());
		 updateButton.setOnClickListener(new updateOnClicklistner());
		 
	 }
	 
	 
	 /*创建类实现监听事件*/
	 
	 //创建数据库
	 class createDatabaseOnClicklistner implements OnClickListener{
		 public void onClick(View v){
			 DatabaseHelper dbHelper=new DatabaseHelper(SqliteActivity.this,"test_database",1);
			 SQLiteDatabase sqliteDatabase=dbHelper.getWritableDatabase();
			 long size=0;
			 
			 //数据库插入原始数据
			 sqliteDatabase.beginTransaction();//开启插入数据事务
			 for(int i=0;i<=10;i++){
				 ContentValues values=new ContentValues();
				 values.put("id", Integer.valueOf(i));
				 values.put("name", "John");
				 sqliteDatabase.insert("user", null, values);
				 size=i;
			 }

			 sqliteDatabase.setTransactionSuccessful();//设置事务处理成功，不设置会自动回滚不提交
			 sqliteDatabase.endTransaction();//在setTransactionSuccessful和endTransaction之间不进行任何数据库操作
			 

			 Toast.makeText(getApplicationContext(),"已建立"+size+"行的数据库", Toast.LENGTH_SHORT).show();
			 dbHelper.close();
		 }
	 }
	 
	 //更新数据库版本
	 class updateDatabaseOnClicklistner implements OnClickListener{
		 public void onClick(View v){
			 //DatabaseHelper dbHelper=new DatabaseHelper(SqliteActivity.this,"test_database",2);
			 //SQLiteDatabase sqliteDatabase=dbHelper.getReadableDatabase();
			
			 Toast.makeText(getApplicationContext(),"数据库已更新", Toast.LENGTH_SHORT).show();
		 }
	 }
	 
	 //插入数据
	class insertOnClicklistner implements OnClickListener{
		 public void onClick(View v){
			 
			 ContentValues value=new ContentValues();
			 value.put("id", 999);
			 value.put("name", "ssss");
			 
			 DatabaseHelper dbHelper=new DatabaseHelper(SqliteActivity.this,"test_database",1);
			 SQLiteDatabase sqliteDatabase=dbHelper.getWritableDatabase();
			 sqliteDatabase.insert("user", null, value);
			 
			 Toast.makeText(getApplicationContext(), "数据已插入", Toast.LENGTH_SHORT).show();
		 }
	 }
	 
	 //查询数据
    class selectOnClicklistner implements OnClickListener{
    	public void onClick(View v){
    		String id;
    		String name;
    		
    		//建立数据库连接并打开
    		DatabaseHelper databaseHelper=new DatabaseHelper(SqliteActivity.this,"test_database",1);
    		SQLiteDatabase sqliteDatabase=databaseHelper.getReadableDatabase();
    		
    		Cursor cursor=sqliteDatabase.query("user", new String[]{"id","name"}, 
    				null, null, null, null, null);
            
    		while(cursor.moveToNext()){
    			id=cursor.getString(cursor.getColumnIndex("id")); 
    			name=cursor.getString(cursor.getColumnIndex("name"));
    			
    			//Log.v("xxx", "id:"+id +"   name:"+name);
    			map=new HashMap<String,Object>();
    			map.put("id", Integer.valueOf(id));
    			map.put("name", name);
    			list.add(map);
    			
    		}
    		databaseHelper.close();
    		cursor.close();
    		
    		mAdapter=new listViewAdapter(SqliteActivity.this,list);
    		lv_showSQLitedata.setAdapter(mAdapter);
  
    		Toast.makeText(getApplicationContext(), "数据来源于创建的数据库", Toast.LENGTH_SHORT).show();
		 }
	 }
    
    //删除数据
     class deleteOnClicklistner implements OnClickListener{
    	public void onClick(View v){
			 DatabaseHelper databaseHelper=new DatabaseHelper(SqliteActivity.this,"test_database",1);
			 SQLiteDatabase sqliteDatabase=databaseHelper.getWritableDatabase();
			 
			 sqliteDatabase.delete("user", "id=?", new String[]{"1"});
			 Toast.makeText(getApplicationContext(), "已删除指定ID", Toast.LENGTH_SHORT).show();
		 }
	 }
    
    //更新数据
    class updateOnClicklistner implements OnClickListener{
    	public void onClick(View v){
			 DatabaseHelper dbHelper=new DatabaseHelper(SqliteActivity.this,"test_database",1);
			 SQLiteDatabase sqliteDatabase=dbHelper.getWritableDatabase();
			 
			 ContentValues value=new ContentValues();
			 value.put("name", "泉水");
			 
			 sqliteDatabase.update("user", value, "id=?", new String[]{"1"});
			 Toast.makeText(getApplicationContext(), "id数据已更新", Toast.LENGTH_SHORT).show();
		 } 
	 }
    
    //返回首页
	 public boolean OnKeyDown(int keyCode,KeyEvent event){
		  if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {   
	           
	        	SqliteActivity.this.finish();
	            overridePendingTransition(R.anim.new_dync_in_from_left, R.anim.new_dync_out_to_right);
	        }      
	        return false;  
	 }

}
